import { CACHE_KEY } from "@/utils/constant"; // 常量

const state = {
  userInfo: {}, // 用户信息
  token: "",
  isRequestedUserInfo: false, // 已经发起过请求用户信息的请求
};
const mutations = {
  // 设置用户信息
  setUserInfo(state, data) {
    state.userInfo = data;
  },
  // 设置token
  setToken(state, data) {
    uni.setStorageSync(CACHE_KEY.TOKEN, data);
    state.token = data;
  },
  // 设置isRequestedUserInfo标识
  setIsRequestedUserInfo(state, data) {
    state.isRequestedUserInfo = data;
  },
};

const actions = {
  // 退出登录
  logout({ commit }) {
    commit("setToken", "");
    commit("setUserInfo", "");
  },
  // 获取Storage里的token
  async GetToken({ commit }) {
    const token = uni.getStorageSync(CACHE_KEY.TOKEN);
    commit("setToken", token);
  },
};

const getters = {};

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters,
};
